package com.example.j20240317.demos.web.service;
import com.example.j20240317.demos.web.enums.BookStatusEnums;
import com.example.j20240317.demos.web.mapper.BookInfoMapper;
import com.example.j20240317.demos.web.model.BookInfo;
import com.example.j20240317.demos.web.model.PageRequest;
import com.example.j20240317.demos.web.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
public class BookService {

    @Autowired
    private BookInfoMapper bookInfoMapper;


    public Integer insertBook(BookInfo bookInfo)
    {
        return bookInfoMapper.insertBook(bookInfo);
    }

    public PageResult<BookInfo> getListByPage (PageRequest pageRequest){

        Integer count = bookInfoMapper.count();
        List<BookInfo> bookInfos = bookInfoMapper.queryListByPage(pageRequest);
//        PageResult<BookInfo> result = new PageResult<>();
//        result.setCount(count);
//        result.getRecords(bookInfos);

for(BookInfo bookInfo:bookInfos){

    bookInfo.setStateCN(BookStatusEnums.getDescByCode(bookInfo.getStatus()).getDesc());
}

        return new PageResult<>(bookInfos,count,pageRequest);
    }

    public BookInfo queryBookById(Integer bookId) {
        return bookInfoMapper.queryBookById(bookId);
    }

    public Integer updateBook(BookInfo bookInfo) {
        return bookInfoMapper.updateBook(bookInfo);
    }

    public Integer batchDelete(List<Integer> ids) {
        return bookInfoMapper.batchDelete(ids);
    }
}
